package org.adream.account.rest.api;

import java.util.List;

import org.adream.account.entity.EducationEntity;
import org.adream.account.model.ResultModel;
import org.adream.account.service.EducationService;
import org.adream.account.util.Utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@RequestMapping("/edu")
@Controller
public class EducationApiController {
	
	@Autowired
	private EducationService eduService;
	
	private final static Logger logger = LoggerFactory.getLogger(EducationApiController.class);
	
	/**
	 * 新增
	 * @param education
	 * @return
	 */
	@RequestMapping(value = "", produces = "application/json;charset=UTF-8", method = RequestMethod.POST)
	@ResponseBody
	public ResultModel<String> addEducation(@RequestBody EducationEntity education) {
		if(education == null || StringUtils.isEmpty(education.getEname())) {
			return new ResultModel<String>(ResultModel.ERR_PARAM, "新增失败,请求参数有误", null);
		}
		return eduService.addEducation(education);
	}
	
	/**
	 * 删除
	 * @param eid
	 * @return
	 */
	@RequestMapping(value = "/{eid}", produces = "application/json;charset=UTF-8", method = RequestMethod.DELETE)
	@ResponseBody
	public ResultModel<String> deleteEducation(@PathVariable("eid") Integer eid) {
		if(eid == null) {
			return new ResultModel<String>(ResultModel.ERR_PARAM, "删除失败,请求参数有误", null);
		}
		return eduService.deleteEducation(eid);
	}
	
	/**
	 * 更新
	 * @param eid
	 * @param educationEntity
	 * @return
	 */
	@RequestMapping(value = "/{eid}", produces = "application/json;charset=UTF-8", method = RequestMethod.PUT)
	@ResponseBody
	public ResultModel<String> updateEducation(@PathVariable("eid") Integer eid,@RequestBody EducationEntity educationEntity) {
		if(eid == null || educationEntity == null || StringUtils.isEmpty(educationEntity.getEname())) {
			return new ResultModel<String>(ResultModel.ERR_PARAM, "更新失败,请求参数有误", null);
		}
		return eduService.updateEducation(eid, educationEntity);
	}
	
	/**
	 * 根据eid,删除标志查询
	 * @param eid
	 * @param dr
	 * @return
	 */
	@RequestMapping(value = "/eid/{eid}/dr/{dr}", produces = "application/json;charset=UTF-8", method = RequestMethod.GET)
	@ResponseBody
	public ResultModel<EducationEntity> queryEduByEidAndDr(@PathVariable("eid") Integer eid,@PathVariable("dr") Integer dr) {
		if(eid == null || dr == null) {
			return new ResultModel<EducationEntity>(ResultModel.ERR_PARAM, "查询失败,请求参数有误", null);
		}
		EducationEntity education = eduService.queryEduByEidAndDr(eid, dr);
		if(education == null) {
			logger.warn("教育程度数据不存在,eid:{}", eid);
			return new ResultModel<EducationEntity>(ResultModel.NOT_FOUND_OBJECT, "查询失败,数据不存在", null);
		}
		return new ResultModel<EducationEntity>(ResultModel.SUCCESS, ResultModel.SUCCESS_MSG, education);
	}
	
	/**
	 * 根据删除标志查询
	 * @param dr 删除标志
	 * @return
	 */
	@RequestMapping(value = "/dr/{dr}", produces = "application/json;charset=UTF-8", method = RequestMethod.GET)
	@ResponseBody
	public ResultModel<List<EducationEntity>> queryEducationByDr(@PathVariable("dr") Integer dr) {
		if(dr == null) {
			return new ResultModel<List<EducationEntity>>(ResultModel.ERR_PARAM, "查询失败,请求参数有误", null);
		}
		List<EducationEntity> educations = eduService.queryEducationByDr(dr);
		if(Utils.isEmptyCollection(educations)) {
			return new ResultModel<List<EducationEntity>>(ResultModel.NOT_FOUND_OBJECT, "查询失败,数据不存在", null);
		}
		return new ResultModel<List<EducationEntity>>(ResultModel.SUCCESS, ResultModel.SUCCESS_MSG, educations);
	}
}